package com.yanfan.maintain.controller;

import com.yanfan.maintain.domain.CalendarBody;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.maintain.domain.*;
import com.yanfan.maintain.service.InspectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author caijinkang
 * @date 2023/3/9 15:01
 */
@RestController
@RequestMapping("/device")
@Api(tags = "设备巡检管理")
public class InspectionController extends BaseController {

    @Resource
    private InspectionService inspectionService;

    @PostMapping("/findTaskRecord")
    @ApiOperation("巡检任务记录")
    public AjaxResult findTaskRecord(@ApiParam(name = "inspectionTask", value = "巡检任务列表") @RequestBody InspectionTask inspectionTask) {
        startPage();
        return inspectionService.findTaskRecord(inspectionTask);
    }

    @PostMapping("/findPlanRecord")
    @ApiOperation("查询巡检计划信息")
    public AjaxResult findPlanRecord(@ApiParam(name = "inspectionTask", value = "巡检计划列表") @RequestBody InspectionPlan inspectionPlan) {
        return inspectionService.findPlanRecord(inspectionPlan);
    }

    @PostMapping("/findInspectionRoute")
    @ApiOperation("查询设备巡检路线")
    public AjaxResult findInspectionRoute(@ApiParam(name = "inspectionBody", value = "设备巡检路线入参") @RequestBody InspectionBody inspectionBody) {
        return inspectionService.findInspectionRoute(inspectionBody);
    }

    @GetMapping("/findClockingRoute")
    @ApiOperation("查询设备打卡点")
    public AjaxResult findClockingRoute() {
        return inspectionService.findClockingRoute();
    }

    @GetMapping("/findClockingRouteCheck")
    @ApiOperation("查询设备检查项")
    public AjaxResult findClockingRouteCheck() {
        return inspectionService.findClockingRouteCheck();
    }

    @PostMapping("/addInspectionRoute")
    @ApiOperation("新增设备巡检路线")
    public AjaxResult addInspectionRoute(@ApiParam(name = "inspectionRoute", value = "设备巡检路线入参") @RequestBody InspectionRoute inspectionRoute) {
        return inspectionService.addInspectionRoute(inspectionRoute);
    }

    @PostMapping("/updateInspectionRoute")
    @ApiOperation("更新设备巡检路线")
    public AjaxResult updateInspectionRoute(@ApiParam(name = "inspectionRoute", value = "设备巡检路线入参") @RequestBody InspectionRoute inspectionRoute) {
        return inspectionService.updateInspectionRoute(inspectionRoute);
    }

    @GetMapping("/deleteInspectionRoute")
    @ApiOperation("根据id删除设备巡检路线信息")
    public AjaxResult deleteInspectionRoute(@RequestParam Integer id) {
        return inspectionService.deleteInspectionRoute(id);
    }

    @PostMapping("/findInspectionPlan")
    @ApiOperation("查询巡检计划列表")
    public AjaxResult findInspectionPlan(@ApiParam(name = "inspectionPlanBody", value = "巡检计划列表入参") @RequestBody InspectionPlanBody inspectionPlanBody) {
        return inspectionService.findInspectionPlan(inspectionPlanBody);
    }

    @PostMapping("/findInspectionPlanTwo")
    @ApiOperation("查询任务里的任务详情->巡检计划列表")
    public AjaxResult findInspectionPlanTwo(@ApiParam(name = "inspectionPlanBody", value = "巡检计划列表入参") @RequestBody InspectionPlanBody inspectionPlanBody) {
        return inspectionService.findInspectionPlanTwo(inspectionPlanBody);
    }

    @GetMapping("/findInspectionRule")
    @ApiOperation("查询巡检规则")
    public AjaxResult findInspectionRule() {
        return inspectionService.findInspectionRule();
    }

    @PostMapping("/addInspectionPlan")
    @ApiOperation("新增巡检计划列表")
    public AjaxResult addInspectionPlan(@ApiParam(name = "inspectionPlan", value = "巡检计划列表入参") @RequestBody InspectionPlan inspectionPlan) {
        return inspectionService.addInspectionPlan(inspectionPlan);
    }

    @PostMapping("/updateInspectionPlan")
    @ApiOperation("更新巡检计划列表")
    public AjaxResult updateInspectionPlan(@ApiParam(name = "inspectionPlan", value = "设备巡检路线入参") @RequestBody InspectionPlan inspectionPlan) {
        return inspectionService.updateInspectionPlan(inspectionPlan);
    }

    @GetMapping("/deleteInspectionPlan")
    @ApiOperation("根据id删除巡检计划信息")
    public AjaxResult deleteInspectionPlan(@RequestParam Integer id) {
        return inspectionService.deleteInspectionPlan(id);
    }

    @PostMapping("/findInspectionTask")
    @ApiOperation("查询巡检任务列表")
    public AjaxResult findInspectionTask(@ApiParam(name = "inspectionTaskBody", value = "巡检任务列表入参") @RequestBody InspectionTaskBody inspectionTaskBody) {
        return inspectionService.findInspectionTask(inspectionTaskBody);
    }

    @PostMapping("/addInspectionTask")
    @ApiOperation("新增巡检任务列表")
    public AjaxResult addInspectionTask(@ApiParam(name = "inspectionTask", value = "巡检任务列表入参") @RequestBody InspectionTask inspectionTask) {
        return inspectionService.addInspectionTask(inspectionTask);
    }

    @PostMapping("/updateInspectionTask")
    @ApiOperation("更新巡检任务列表")
    public AjaxResult updateInspectionTask(@ApiParam(name = "inspectionTask", value = "巡检任务列表入参") @RequestBody InspectionTask inspectionTask) {
        return inspectionService.updateInspectionTask(inspectionTask);
    }

    @GetMapping("/deleteInspectionTask")
    @ApiOperation("根据id删除巡检任务信息")
    public AjaxResult deleteInspectionTask(@RequestParam Integer id) {
        return inspectionService.deleteInspectionTask(id);
    }

    @GetMapping("/findInspectionTaskChart")
    @ApiOperation("巡检任务分布饼状图")
    public AjaxResult findInspectionTaskChart(@RequestParam String range) {
        return inspectionService.findInspectionTaskChart(range);
    }

    @GetMapping("/findInspectionFlawChart")
    @ApiOperation("巡检缺陷饼状图")
    public AjaxResult findInspectionFlawChart(@RequestParam String range) {
        return inspectionService.findInspectionFlawChart(range);
    }

//    @GetMapping("/findInspectionReportedChart")
//    @ApiOperation("巡检上报柱状图")
//    public AjaxResult findInspectionReportedChart(@RequestParam String range) {
//        return inspectionService.findInspectionReportedChart(range);
//    }

    @PostMapping("/findInspectionCalendar")
    @ApiOperation("查询巡检日历")
    public AjaxResult findInspectionCalendar(@ApiParam(name = "calendarBody", value = "巡检任务列表入参") @RequestBody CalendarBody calendarBody) {
        return inspectionService.findInspectionCalendar(calendarBody);
    }

    @GetMapping("/findInspectionPlanTemplate")
    @ApiOperation("查询巡检计划模板")
    public AjaxResult findInspectionPlanTemplate() {
        return inspectionService.findInspectionPlanTemplate();
    }

    @PostMapping("/inspectionCalendarByReport")
    @ApiOperation("下载巡检日程")
    public void inspectionCalendarByReport(HttpServletResponse response, @ApiParam(name = "calendarBody", value = "巡检任务列表入参") @RequestBody CalendarBody calendarBody) {
        inspectionService.inspectionCalendarByReport(response, calendarBody);
    }

    @GetMapping("/findSystemComboBox")
    @ApiOperation("查询系统信息下拉框")
    public AjaxResult findSystemComboBox() {
        return inspectionService.findSystemComboBox();
    }

    @GetMapping("/findArtsVisionComboBox")
    @ApiOperation("查询公司下拉框信息")
    public AjaxResult findArtsVisionComboBox() {
        return inspectionService.findArtsVisionComboBox();
    }

    @GetMapping("/findCategoryComboBox")
    @ApiOperation("查询设备类别下拉框信息")
    public AjaxResult findCategoryComboBox() {
        return inspectionService.findCategoryComboBox();
    }

    @GetMapping("/findGroupComboBox")
    @ApiOperation("查询设备分组下拉框信息")
    public AjaxResult findGroupComboBox() {
        return inspectionService.findGroupComboBox();
    }

    @GetMapping("/findInspectionLine")
    @ApiOperation("查询巡检路线")
    public AjaxResult findInspectionLine() {
        return inspectionService.findInspectionLine();
    }


    @GetMapping("/findInspectionCheckByLine")
    @ApiOperation("根据巡检路线查询路线和检查项")
    public AjaxResult findInspectionCheckByLine(@RequestParam Integer id) {
        return inspectionService.findInspectionCheckByLine(id);
    }

    @GetMapping("/findInspectionPlanComboBox")
    @ApiOperation("查询巡检计划下拉框信息")
    public AjaxResult findInspectionPlanComboBox() {
        return inspectionService.findPlanComboBox();
    }

    @GetMapping("/findRouteByDeviceId")
    @ApiOperation("根据设备id查询路线")
    public AjaxResult findRouteByDeviceId(@RequestParam Integer deviceId) {
        return inspectionService.findRouteByDeviceId(deviceId);
    }

    @GetMapping("/findPlacement")
    @ApiOperation("查询安装机位下拉框信息")
    public AjaxResult findPlacement() {
        return AjaxResult.success("查询成功", inspectionService.findPlacement());
    }

    @GetMapping("/findPlacementByUser")
    @ApiOperation("查询安装机位下拉框信息（权限查看）")
    public AjaxResult findPlacementByUser() {
        return AjaxResult.success("查询成功", inspectionService.findPlacementByUser());
    }


    /**
     * 根据巡检任务ID查询巡检任务的项目路线信息。
     *
     * @param id 巡检任务的ID，用于指定要查询的巡检任务。
     * @return 返回查询到的巡检任务的项目路线信息。
     * @GetMapping("/findInspectionTaskRouteLog") 用于指定HTTP请求方法和请求路径。
     * @ApiOperation("查询巡检任务的项目路线") 用于描述该接口的功能。
     */
    @GetMapping("/findInspectionTaskRouteLog")
    @ApiOperation("查询巡检任务的项目路线")
    public AjaxResult findInspectionTaskRouteLog(@RequestParam Long id) {
        return inspectionService.findInspectionTaskRouteLog(id);
    }

    /**
     * 修改巡检任务的项目路线。
     * 通过接收InspectionRoute对象，更新巡检任务的路线信息。
     * 此接口用于在后台管理系统中，允许管理员更改已存在的巡检任务所关联的项目路线。
     *
     * @param route 包含新路线信息的InspectionRoute对象。
     * @return 返回操作结果的AjaxResult对象，包含成功与否、错误信息等。
     */
    @PostMapping("/updateInspectionTaskRouteLog")
    @ApiOperation("修改巡检任务的项目路线")
    public AjaxResult updateInspectionTaskRouteLog(@RequestBody InspectionRoute route) {
        return inspectionService.updateInspectionTaskRouteLog(route);
    }


    /**
     * 巡检任务项审核
     *
     * @return
     */
    @PostMapping("/checkedInspectionTaskItems")
    public AjaxResult checkedInspectionTaskItems(@RequestBody CheckInspectionTaskRouteLogVO check) {
        int i = this.inspectionService.checkedInspectionTaskItems(check.getTaskId(), check.getSerialNumber());
        return toAjax(i);
    }

    @Getter
    @Setter
    public static class CheckInspectionTaskRouteLogVO {
        public Long taskId;
        public String serialNumber;
    }

}
